<?php
/**
*
* @package phpBB3
* @version $Id$
* @copyright (c) 2005 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
*/

/**
* @ignore
*/
 
define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include_once($phpbb_root_path . 'common.' . $phpEx);
include_once($phpbb_root_path . 'includes/functions_display.' . $phpEx);
include_once($phpbb_root_path . 'sv_common.' . $phpEx);
include_once($phpbb_root_path . 'prophet_common.' . $phpEx);




// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup('stockvirtual');


//$debug = true;

$analyse_local_report		= request_var('analyse_local_report', '');
if ($analyse_local_report == 1)
{
	$report_path		= request_var('report_path', '');
	
	analyse_local_report($report_path);	
}



display_forums('', $config['load_moderators']);

// include nav links.
include_nav_links();

// Assign index specific vars
$template->assign_vars(array(

	'FORUM_IMG'				=> $user->img('forum_read', 'NO_NEW_POSTS'),
	'FORUM_NEW_IMG'			=> $user->img('forum_unread', 'NEW_POSTS'),
	'FORUM_LOCKED_IMG'		=> $user->img('forum_read_locked', 'NO_NEW_POSTS_LOCKED'),
	'FORUM_NEW_LOCKED_IMG'	=> $user->img('forum_unread_locked', 'NO_NEW_POSTS_LOCKED'),

	'S_LOGIN_ACTION'			=> append_sid("{$phpbb_root_path}ucp.$phpEx", 'mode=login'),
	'S_DISPLAY_BIRTHDAY_LIST'	=> ($config['load_birthdays']) ? true : false,
	'U_MONEY'					=> number_format($money),
	
	'TAIEX_FIRST_DATE'			=> $TAIEX_first_date,
	'TAIEX_LAST_DATE'			=> $TAIEX_last_date,
	'LOCAL_REPORT_PATH'			=> $local_report_path,
	'LOCAL_RESULT_PATH'			=> $report_path,
		
	'U_MARK_FORUMS'		=> ($user->data['is_registered'] || $config['load_anon_lastread']) ? append_sid("{$phpbb_root_path}index.$phpEx", 'hash=' . generate_link_hash('global') . '&amp;mark=forums') : '',
	'U_MCP'				=> ($auth->acl_get('m_') || $auth->acl_getf_global('m_')) ? append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=main&amp;mode=front', true, $user->session_id) : '')
	
	
);

// Output page
page_header($user->lang['SV_RULE']);

$template->set_filenames(array(
	'body' => 'prophet_analysis.html')
);

page_footer();



function analyse_local_report($report_path)
{
	global $local_result_path, $db, $debug, $template;	
		
	$model_id = array('A', 'B', 'C', 'D', 'E', 'F');
		
	$sell_signal	= array();
	$buy_signal		= array();
	
	// overall statistic data per model about prediction accuracy	
	$total_observation_num 					= 0;
	$total_peak_num							= array();
	$total_predicted_peak_num				= array();
	$total_T1_error_predicted_peak_num		= array();		// false alarm, False positive error 
	$total_T2_error_predicted_peak_num		= array();		// a miss, False negative error
	$total_valley_num						= array();
	$total_predicted_valley_num				= array();
	$total_T1_error_predicted_valley_num	= array();		// false alarm, False positive error 
	$total_T2_error_predicted_valley_num	= array();		// a miss, False negative error
	
	// statistic data per corp about prediction accuracy	
	$observation_num_per_corp				= array();
	$peak_num_per_corp						= array();
	$predicted_peak_num_per_corp			= array();
	$peak_T1_error_num_per_corp				= array();		// false alarm, False positive error 
	$peak_T2_error_num_per_corp				= array();		
	$valley_num_per_corp					= array();
	$predicted_valley_num_per_corp			= array();
	$valley_T1_error_num_per_corp			= array();		// false alarm, False positive error 
	$valley_T2_error_num_per_corp			= array();		
	
	// statistic data about profitability 
	//	peak: the period with peak signals
	//	valley: the period with valley signals
	//	gap:	the priod with neutral signals. We only count the gaps between a peak period and a valley, since we cannot calculate the profit from the first and the last gap. 
	// 	
	$price_change_before_after_gap			= array();		// the difference of the first and the last price during a gap period. This is to show the profitability. 
	$days_of_price_change_before_after_gap	= array();
	
	// statistic data about risk 
	$price_sum_peak							= array();
	$price_sum_valley						= array();
	$price_variance_sum_peak				= array();
	$price_variance_sum_valley				= array();
		
	// trading simulation
	
	
	
	if ($report_path == '')
	{
		$report_path = $local_result_path;
	}
	
	if (file_exists($report_path) == false)
	{
		if ($debug)
			echo "result path not exist<BR>\n";
		return;
	}
	
	//
	//	read through csv reports and collect data
	//
	
	$all_corp 			= get_all_corp_id();
	
	$all_corp_id 		= array();
	$sell_signal_prob	= array();
	$buy_signal_prob	= array();
	
	foreach($all_corp as $corp_id)
	{		
		$path = $report_path . $corp_id . '.csv';

		if (file_exists($path) == false)
		{
//			if ($debug)
//				echo "$path not exist<BR>\n";
			continue;
		}		
		if ($debug)
			echo "$path loaded <BR>\n";		
		
		//
		//	Load csv
		//
		$raw_file 	= file_get_contents($path);
		$csv 		= sv_str_getcsv ($raw_file, "\n");    
		
		

		//
		//	Find the Corps with Non-Neutral Signals in the Last Day 
		//
		
		// check if there is buy or sell signal in the last row 		
		// get last row
		$i = count($csv) - 1;
//		echo "$i rows<BR>\n";
//    	print_r($csv[$i]);
		if ($debug)
    		echo "$i rows<BR>\n";
    	
		// in case some info is missing, e.g. TAIEX in the latest day, we look back for a valid output 
    	while($i > 0)
		{
    		$row = sv_str_getcsv($csv[$i][0]);
    		$row = $row[0];
    	
    		if ($row[25] == '' && $row[26] == '' && $row[27] == '')
	    		$i -= 1;
    		else
    			break;
		}    	
    	
//    	echo "$i-th row selected <BR>\n";
//    	print_r($row);
//    	echo "<BR>\n";    			
		
		$neutral_signal = true;
		// check if non-neutral results
		if ($row[26] == constant('PEAK') || $row[27] == constant('PEAK') || $row[28] == constant('PEAK') || 
			$row[29] == constant('PEAK') || $row[30] == constant('PEAK') || $row[31] == constant('PEAK') )
		{						
			$sell_signal_prob[$corp_id] 	= array();
			$sell_signal_prob[$corp_id][] 	= round($row[34] * 100, 0);
			$sell_signal_prob[$corp_id][] 	= round($row[37] * 100, 0);
			$sell_signal_prob[$corp_id][] 	= round($row[40] * 100, 0);
			$sell_signal_prob[$corp_id][] 	= round($row[43] * 100, 0);
			$sell_signal_prob[$corp_id][] 	= round($row[46] * 100, 0);
			$sell_signal_prob[$corp_id][] 	= round($row[49] * 100, 0);
			
			$sell_signal[$corp_id] = max($sell_signal_prob[$corp_id]);
			
			$neutral_signal = false;
		}
		
		if ($row[26] == constant('VALLEY') || $row[27] == constant('VALLEY') || $row[28] == constant('VALLEY') || 
			$row[29] == constant('VALLEY') || $row[30] == constant('VALLEY') || $row[31] == constant('VALLEY') )
		{			
			$buy_signal_prob[$corp_id] 		= array();
			$buy_signal_prob[$corp_id][] 	= round($row[32] * 100, 0);
			$buy_signal_prob[$corp_id][] 	= round($row[35] * 100, 0);
			$buy_signal_prob[$corp_id][] 	= round($row[38] * 100, 0);
			$buy_signal_prob[$corp_id][] 	= round($row[41] * 100, 0);
			$buy_signal_prob[$corp_id][] 	= round($row[44] * 100, 0);
			$buy_signal_prob[$corp_id][] 	= round($row[47] * 100, 0);
			
			$buy_signal[$corp_id] = max($buy_signal_prob[$corp_id]);
			
			$neutral_signal = false;
		}
		
		// if the latest signal is not neutral, like buy or sell, collect data
		if ($neutral_signal == false)
		{
			// accuracy data 
			$observation_num_per_corp[$corp_id]				= 0;
			$peak_num_per_corp[$corp_id]					= array();
			$predicted_peak_num_per_corp[$corp_id]			= array();
			$peak_T1_error_num_per_corp[$corp_id]			= array();		// false alarm, False positive error 
			$peak_T2_error_num_per_corp[$corp_id]			= array();		// false alarm, False positive error 
			$valley_num_per_corp[$corp_id]					= array();
			$predicted_valley_num_per_corp[$corp_id]		= array();
			$valley_T1_error_num_per_corp[$corp_id]			= array();		// false alarm, False positive error 
			$valley_T2_error_num_per_corp[$corp_id]			= array();		// false alarm, False positive error 
			
			// profitability and risk data			
			$price_change_before_after_gap[$corp_id]		= array();		// the difference of the first and the last price during a gap period. This is to show the profitability. 
			$days_of_price_change_before_after_gap[$corp_id]= array();
						
			
			// risk data
			$price_sum_peak[$corp_id]						= array();
			$price_sum_valley[$corp_id]						= array();
			$price_variance_sum_peak[$corp_id]				= array();
			$price_variance_sum_valley[$corp_id]			= array();
		}
		
		
		//
		// collect data for statistics , all corps in total
		// 
		$i = 0;
		
		// analyse classification correct rate
		foreach($csv as $row)
		{
			$row = sv_str_getcsv($row[0]);
    		$row = $row[0];
    		
    		++$i;
    		
    		// if this obervation has no predicted value
    		if ($row[26] != constant('PEAK') && $row[26] != constant('NEUTRAL') && $row[26] != constant('VALLEY'))
    		{
//	    		if ($debug)
//	    			echo "skip this row $i due to no predicted values <BR>". PHP_EOL;
    			continue;
			}
    			
    		// skip training set, 7:3 is the default setting in SPSS. 
    		if ($i < 0.7 * count($csv))
    		{
//	    		if ($debug)
//	    			echo "skip this row $i due to that this row is in the training set <BR>". PHP_EOL;
    			continue;  			
			}
    			

    		// statistic data for all corps
    		++$total_observation_num;
    		
    		for($k = 0;$k < 6; ++$k)
    		{	
	    		$model = $model_id[$k];
	    		
	    		// count real peaks
	    		if ($row[19 + $k] == constant('PEAK'))	
	    		{
	    			++$total_peak_num[$model];	
	    			
	    			$price_sum_peak[$corp_id][$model] += $row[6];
    			}
	    		
    			// count predicted peaks which are real
	    		if ($row[19 + $k] == constant('PEAK') && $row[26 + $k] == constant('PEAK'))	
	    		{
	    			++$total_predicted_peak_num[$model];
    			}
	    		
	    		//	count flase alarm, type 1 error
	    		if ($row[19 + $k] != constant('PEAK') && $row[26 + $k] == constant('PEAK'))	
	    		{
	    			++$total_T1_error_predicted_peak_num[$model];	
    			}
	    		
	    		// count miss, type 2 error
    			if ($row[19 + $k] == constant('PEAK') && $row[26 + $k] != constant('PEAK'))	
    			{
    				++$total_T2_error_predicted_peak_num[$model];
				}

    			if ($row[19 + $k] == constant('VALLEY'))	
    			{
    				++$total_valley_num[$model];
    				
    				$price_sum_valley[$corp_id][$model] += $row[6];
				}
    			
    			if ($row[19 + $k] == constant('VALLEY') && $row[26 + $k] == constant('VALLEY'))	
    			{
    				++$total_predicted_valley_num[$model];
				}
    			
    			if ($row[19 + $k] != constant('VALLEY') && $row[26 + $k] == constant('VALLEY'))	
    			{
    				++$total_T1_error_predicted_valley_num[$model];	
				}
    			
    			if ($row[19 + $k] == constant('VALLEY') && $row[26 + $k] != constant('VALLEY'))	
    			{
    				++$total_T2_error_predicted_valley_num[$model];	
				}
    		}
    		  		
		}	// analyse classification correct rate, all corps in total
		
		// continue to next corp, if this corp has neutral signal
		if ($neutral_signal)
			continue;
		
				
		//
		// collect data for statistics , only the corps with non-neutral signals 
		// 
		
		$i 		= 0;
		
		$stage						= array();		// we only count the gaps between a peak period and a valley. 
		$price_in_gap_beginning		= array();		// the price of the first day of a gap period
		$days_during_the_gap		= array();		// the length of the gap period which is being calculating
		
		for($k = 0;$k < 6; ++$k)
    	{	
	    	$model 								= $model_id[$k];
	    	$stage[$model]						= constant('PHP_INT_MAX');
	    	$price_in_gap_beginning[$model]		= 0.1;
	    	$days_during_the_gap[$model]		= 0;
	    	
	    	$price_mean_peak[$corp_id][$model]	= $price_sum_peak[$corp_id][$model] / ( count($csv) * 0.7 );
	    	$price_mean_valley[$corp_id][$model]= $price_sum_valley[$corp_id][$model] / ( count($csv) * 0.7 );
    	}
				
		foreach($csv as $row)
		{
			$row = sv_str_getcsv($row[0]);
    		$row = $row[0];
    		
    		++$i;
    		
    		// if this obervation has no predicted value
    		if ($row[26] != constant('PEAK') && $row[26] != constant('NEUTRAL') && $row[26] != constant('VALLEY'))
    			continue;
    			
    		// skip training set, 7:3 is the default setting in SPSS. 
    		if ($i < 0.7 * count($csv))
    			continue;  			
    			
    		++$observation_num_per_corp[$corp_id];

    		// statistic data    		
    		for($k = 0;$k < 6; ++$k)
    		{	
	    		$model = $model_id[$k];
	    			    		
//	    		if ($k == 0)
//	    		{
//		    		$signal = $row[19];
//		    		$s		= $stage[$model];
//		    		$p		= $price_in_gap_beginning[$model];
//		    		$d		= $days_during_the_gap[$model];
//		    		
//		    		echo "	$model, $i: signal:$signal, stage:$s price_in_gap_beginning:$p days_during_the_gap:$d <BR>" . PHP_EOL;
//		    		
//	    		}
	    		
	    		// count real peaks
	    		if ($row[19 + $k] == constant('PEAK'))	
	    		{
	    			++$peak_num_per_corp[$corp_id][$model];     			
	    			
	    			$price_variance_sum_peak[$corp_id][$model] += pow(($price_mean_peak[$corp_id][$model] - $row[6]), 2);
	    			
	    			// check stage status  			
	    			if ($stage[$model] == constant('NEUTRAL'))
	    			{
		    			if ($price_in_gap_beginning[$model] == 0 && $debug)
		    			{
			    			echo "closing price: " . $row[6] . " price_in_gap_beginning: " .  $price_in_gap_beginning[$model] . ' <BR> ' . PHP_EOL;
			    			$diff = abs($row[6] - $price_in_gap_beginning[$model]) * 100 / $price_in_gap_beginning[$model];			    			
		    				$d = $days_during_the_gap[$model];			    			
		    				echo "model $model: $i , diff:$diff, end of a gap period($d days) , start of a peak period  <BR>" . PHP_EOL;		
		    			}
		    			
		    			// end of a gap period, start of a peak period
			    		$stage[$model] = constant('PEAK');
			    		
		    			if ($price_in_gap_beginning[$model] != 0)
		    			{
			    			
			    			$price_change_before_after_gap[$corp_id][$model][] = abs($row[6] - $price_in_gap_beginning[$model]) * 100 / $price_in_gap_beginning[$model];	
			    			$days_of_price_change_before_after_gap[$corp_id][$model][] = $days_during_the_gap[$model];	
						}
	    			}
	    			else if ($stage[$model] == constant('PEAK'))
	    			{
		    			// still in a peak period
		    				
	    			}
	    			else if ($stage[$model] == constant('VALLEY'))
	    			{
		    			//  end of a valley period , start of a peak period
		    			$stage[$model] = constant('PEAK');
	    			}
	    			else
	    			{
		    			// this is the first non-neutral signal.
		    			// start of a peak period		    			
		    			$stage[$model] = constant('PEAK');
	    			}
    			}
	    		
    			// count predicted peaks which are real
	    		if ($row[19 + $k] == constant('PEAK') && $row[26 + $k] == constant('PEAK'))	
	    		{
	    			++$predicted_peak_num_per_corp[$corp_id][$model];	
    			}
	    		
	    		//	count flase alarm, type 1 error
	    		if ($row[19 + $k] != constant('PEAK') && $row[26 + $k] == constant('PEAK'))	
	    		{
	    			++$peak_T1_error_num_per_corp[$corp_id][$model];
    			}
	    		
	    		// count miss, type 2 error
    			if ($row[19 + $k] == constant('PEAK') && $row[26 + $k] != constant('PEAK'))	
    			{
    				++$peak_T2_error_num_per_corp[$corp_id][$model];
				}

				// count real valleies
    			if ($row[19 + $k] == constant('VALLEY'))	
    			{
    				++$valley_num_per_corp[$corp_id][$model]; 
    				
    				$price_variance_sum_valley[$corp_id][$model] += pow(($price_mean_valley[$corp_id][$model] - $row[6]), 2);
    				    				
    				// check stage status
    				if ($stage[$model] == constant('NEUTRAL'))	
	    			{
		    			if ($price_in_gap_beginning[$model] == 0 && $debug)
		    			{
			    			echo "closing price: " . $row[6] . " price_in_gap_beginning: " .  $price_in_gap_beginning[$model] . ' <BR> ' . PHP_EOL;
			    			$diff = abs($row[6] - $price_in_gap_beginning[$model]) * 100 / $price_in_gap_beginning[$model];	
		    				$d = $days_during_the_gap[$model];			    			
		    				echo "model $model: $i , diff:$diff, end of a gap period($d days) , start of a valley period  <BR>" . PHP_EOL;	
		    			}
		    			
		    			// end of a gap period , start of a valley period
			    		$stage[$model] = constant('VALLEY');
		    			if ($price_in_gap_beginning[$model] != 0)
		    			{
			    			$price_change_before_after_gap[$corp_id][$model][] = abs($row[6] - $price_in_gap_beginning[$model]) * 100 / $price_in_gap_beginning[$model];	
			    			$days_of_price_change_before_after_gap[$corp_id][$model][] = $days_during_the_gap[$model];	
						}		    			
		    			
	    			}
	    			else if ($stage[$model] == constant('PEAK'))
	    			{
		    			// end of a peak period , start of a valley period
		    			$stage[$model] = constant('VALLEY');
		    			
	    			}
	    			else if ($stage[$model] == constant('VALLEY'))
	    			{
		    			// still in a valley
		    			
	    			}
	    			else
	    			{
		    			// this is the first non-neutral signal.
		    			// start of a valley period
		    			$stage[$model] = constant('VALLEY');
		    				
	    			}
				}
    			
    			if ($row[19 + $k] == constant('VALLEY') && $row[26 + $k] == constant('VALLEY'))	
    			{
    				++$predicted_valley_num_per_corp[$corp_id][$model];
				}
    			
    			if ($row[19 + $k] != constant('VALLEY') && $row[26 + $k] == constant('VALLEY'))	
    			{
    				++$valley_T1_error_num_per_corp[$corp_id][$model];
				}
    			
    			if ($row[19 + $k] == constant('VALLEY') && $row[26 + $k] != constant('VALLEY'))	
    			{
    				++$valley_T2_error_num_per_corp[$corp_id][$model];
				}
				
				// count real gaps(neutral)
    			if ($row[19 + $k] == constant('NEUTRAL'))	
    			{
	    			//$price_during_gap[$corp_id][$model][]	= $row[6];	// closing price
	    			
    				// check stage status
	    			if ($stage[$model] == constant('NEUTRAL'))
	    			{
		    			// still in a gap period 
		    			$days_during_the_gap[$model]	+= 1;
	    			}
	    			else if ($stage[$model] == constant('PEAK'))
	    			{
		    			// start of a gap period, end of a peak period 
		    			$stage[$model] = constant('NEUTRAL');
		    			$price_in_gap_beginning[$model] = $row[6];
		    			$days_during_the_gap[$model]	= 1;
		    			
		    			//$p = $price_in_gap_beginning[$model];
		    			//echo "model $model: $i , price_in_gap_beginning:$p, start of a gap period, end of a peak period  <BR>" . PHP_EOL;
	    			}
	    			else if ($stage[$model] == constant('VALLEY'))
	    			{
		    			// start of a gap period, end of a valley period 
		    			$stage[$model] = constant('NEUTRAL');
		    			$price_in_gap_beginning[$model] = $row[6];		    				
		    			$days_during_the_gap[$model]	= 1;
		    			
		    			//$p = $price_in_gap_beginning[$model];
		    			//echo "model $model: $i , price_in_gap_beginning:$p, start of a gap period, end of a valley period  <BR>" . PHP_EOL;
	    			}
	    			else
	    			{
		    			// it's in the first gap
		    			// there is no non-neutral signal found yet. 
	    			}
				}
    		}
    		  		
		} // collect data for statistics , only the corps with non-neutral signals 
		
		
	}	// for all corps in the set 
	
	
//	echo "price_change_before_after_gap: <BR>";
//	print_r($price_change_before_after_gap);
//	echo "price_change_before_after_gap: <BR>";
	


	//
	// calculate total overall statistics for each model
	//	
	
	$signal_percent		= array();	
	$accuracy			= array();
	$error_rate 		= array();
	
	$peak_accuracy 		= array();
	$peak_T1_rate 		= array();
	$peak_T2_rate 		= array();
	
	$valley_accuracy 	= array();
	$valley_T1_rate 	= array();
	$valley_T2_rate 	= array();
	
	for($k = 0;$k < 6; ++$k)
    {	
		$model = $model_id[$k];
	
		$signal_percent[$model] = ($total_peak_num[$model] + $total_valley_num[$model]) / $total_observation_num;	
		$accuracy[$model] = ($total_predicted_peak_num[$model] + $total_predicted_valley_num[$model]) / ($total_peak_num[$model] + $total_valley_num[$model]);
		$error_rate[$model] = ($total_T1_error_predicted_peak_num[$model] + $total_T2_error_predicted_peak_num[$model] + $total_T1_error_predicted_valley_num[$model] + $total_T2_error_predicted_valley_num[$model]) / ($total_peak_num[$model] + $total_valley_num[$model]);
		
		$peak_accuracy[$model] = $total_predicted_peak_num[$model] / $total_peak_num[$model];
		$peak_T1_rate[$model] = $total_T1_error_predicted_peak_num[$model] / $total_peak_num[$model];	
		$peak_T2_rate[$model] = $total_T2_error_predicted_peak_num[$model] / $total_peak_num[$model];
		
		$valley_accuracy[$model] = $total_predicted_valley_num[$model] / $total_valley_num[$model];
		$valley_T1_rate[$model] = $total_T1_error_predicted_valley_num[$model] / $total_valley_num[$model];
		$valley_T2_rate[$model] = $total_T2_error_predicted_valley_num[$model] / $total_valley_num[$model];
	}
	
	foreach($model_id as $id)
	{
		if ($debug)
		{
			echo "Model $id, $total_observation_num <BR>" . PHP_EOL;
			
			$str = "signal_percent: " . round($signal_percent[$id], 3) . " accuracy: " . round($accuracy[$id], 3) . " error_rate: " . round($error_rate[$id], 3) . '<BR>' . PHP_EOL;
			$str .= " peak_accuracy: " . round($peak_accuracy[$id], 3) . " peak_T1_rate: " . round($peak_T1_rate[$id], 3) . " peak_T2_rate: " . round($peak_T2_rate[$id], 3) . '<BR>' . PHP_EOL;
			$str .= " valley_accuracy: " . round($valley_accuracy[$id], 3) . " valley_T1_rate: " . round($valley_T1_rate[$id], 3) . " valley_T2_rate: " . round($valley_T2_rate[$id], 3) . '<BR> <BR> ' . PHP_EOL;
			
			echo $str;
		}
		
		global $model_parameter;
		
		$model_desc[$id] = 'window: ' . $model_parameter[$id]['window'] . ', step: ' . $model_parameter[$id]['step'] . ', fuzzy: ' . $model_parameter[$id]['fuzzy'] . ', tolerance: ' . $model_parameter[$id]['tolerance'];
	
		$template->assign_block_vars('model_summary', array(
			'MODEL_ID'								=> $id,
			'MODEL_DESC'							=> $model_desc[$id],
			'SIGNAL_PERCENT'						=> round($signal_percent[$id] * 100, 0),		
			'ACCURACY'								=> round($accuracy[$id] * 100, 0),	
			'ERROR_RATE'							=> round($error_rate[$id] * 100, 0),
			'PEAK_ACCURACY'							=> round($peak_accuracy[$id] * 100, 0),	
			'VALLEY_ACCURACY'						=> round($valley_accuracy[$id] * 100, 0),	
			'PEAK_T1_RATE'							=> round($peak_T1_rate[$id] * 100, 0),	
			'PEAK_T2_RATE'							=> round($peak_T2_rate[$id] * 100, 0),	
			'VALLEY_T1_RATE'						=> round($valley_T1_rate[$id] * 100, 0),	
			'VALLEY_T2_RATE'						=> round($valley_T2_rate[$id] * 100, 0),			
			)
		);		
		
	}
	
	
	// merge corps with non-neutral signals
	$corps_with_signals				= $buy_signal + $sell_signal;
	
	
	
	//
	// calculate profitability and risk statistics for each model and each corp 
	//
	
	$monthly_return_rate					= array();
	$total_return_rate						= array();
	$price_std_dev_peak						= array();
	$price_std_dev_valley					= array();
	
	
	foreach($corps_with_signals as $id => $max_prob)
	{
		if ($debug)
			echo "$id - calculate statistics for each model and each corp <BR> observation_num_per_corp:" . $observation_num_per_corp[$id] . '<BR>' . PHP_EOL;
			
		$monthly_return_rate[$id]					= array();
		$total_return_rate[$id]						= array();
		$price_std_dev_peak[$id]					= array();
		$price_std_dev_valley[$id]					= array();
	
		
		for($k = 0;$k < 6; ++$k)
    	{		    	
			$model = $model_id[$k];
			
			if ($debug)
	    		echo "model: $model <BR>" . PHP_EOL;
			
			$return 	= 1;
			$day		= 0;
			
			// accumulate return and days 
			for ($i = 0; $i < count($days_of_price_change_before_after_gap[$id][$model]); ++$i)
			{
				if ($debug)
	    			echo "price change:" . $price_change_before_after_gap[$id][$model][$i] . '% day: ' .  $days_of_price_change_before_after_gap[$id][$model][$i]  . "<BR>" . PHP_EOL;
	    			
				$return *= 	(1 + ($price_change_before_after_gap[$id][$model][$i] / 100));
				$day += 	$days_of_price_change_before_after_gap[$id][$model][$i];				
			}
			
			if ($debug)
				echo "in total, return: $return, day:$day <BR>" . PHP_EOL;
			
			if ($day == 0)
				$monthly_return_rate[$id][$model] = 0;
			else
				$monthly_return_rate[$id][$model] = ($return - 1) * 30 * 100 / $day;
				
			$total_return_rate[$id][$model] = ($return - 1) * 100;
			
			// calculate price std dev during peak/valley periods
			$price_std_dev_peak[$id][$model] = sqrt( $price_variance_sum_peak[$id][$model] / $observation_num_per_corp[$id] );
			$price_std_dev_valley[$id][$model] = sqrt( $price_variance_sum_valley[$id][$model] / $observation_num_per_corp[$id] );
			 
		}
	}
	
	
	
	//
	// calculate accuracy statistics for each model and each corp 
	//
	
	$signal_percent_per_corp		= array();	
	$accuracy_per_corp				= array();
	$error_rate_per_corp 			= array();
	
	$peak_accuracy_per_corp 		= array();
	$peak_T1_rate_per_corp 			= array();
	$peak_T2_rate_per_corp 			= array();
	
	$valley_accuracy_per_corp	 	= array();
	$valley_T1_rate_per_corp 		= array();
	$valley_T2_rate_per_corp 		= array();
	
	
		
	foreach($corps_with_signals as $id => $max_prob)
	{
		if ($debug)
			echo "$id - calculate statistics for each model and each corp <BR> observation_num_per_corp:" . $observation_num_per_corp[$id] . '<BR>' . PHP_EOL;
			
		$template->assign_block_vars('corp_list', array(
			'CORP_ID'								=> $id,
			'CORP_NAME'								=> get_corp_chinese_name($id),	
			)
		);		
		
		$signal_percent_per_corp[$id]		= array();	
		$accuracy_per_corp[$id]				= array();
		$error_rate_per_corp[$id] 			= array();
	
		$peak_accuracy_per_corp[$id] 		= array();
		$peak_T1_rate_per_corp[$id] 		= array();
		$peak_T2_rate_per_corp[$id] 		= array();
	
		$valley_accuracy_per_corp[$id]	 	= array();
		$valley_T1_rate_per_corp[$id] 		= array();
		$valley_T2_rate_per_corp[$id] 		= array();
	
		for($k = 0;$k < 6; ++$k)
    	{	
			$model = $model_id[$k];
	
			$signal_percent_per_corp[$id][$model] = ($peak_num_per_corp[$id][$model] + $valley_num_per_corp[$id][$model]) / $observation_num_per_corp[$id];	
			
			if (($peak_num_per_corp[$id][$model] + $valley_num_per_corp[$id][$model]) == 0)
			{
				$accuracy_per_corp[$id][$model] = 0;
				$error_rate_per_corp[$id][$model] = 0;
			}
			else
			{
				$accuracy_per_corp[$id][$model] = ($predicted_peak_num_per_corp[$id][$model] + $predicted_valley_num_per_corp[$id][$model]) / ($peak_num_per_corp[$id][$model] + $valley_num_per_corp[$id][$model]);
				$error_rate_per_corp[$id][$model] = ($peak_T1_error_num_per_corp[$id][$model] + $peak_T2_error_num_per_corp[$id][$model] + $valley_T1_error_num_per_corp[$id][$model] + $valley_T2_error_num_per_corp[$id][$model]) / ($peak_num_per_corp[$id][$model] + $valley_num_per_corp[$id][$model]);
			}
		
			if ($peak_num_per_corp[$id][$model] == 0)
			{
				$peak_accuracy_per_corp[$id][$model] = 0;
				$peak_T1_rate_per_corp[$id][$model] = 0;	
				$peak_T2_rate_per_corp[$id][$model] = 0;
			}
			else
			{
				$peak_accuracy_per_corp[$id][$model] = $predicted_peak_num_per_corp[$id][$model] / $peak_num_per_corp[$id][$model];
				$peak_T1_rate_per_corp[$id][$model] = $peak_T1_error_num_per_corp[$id][$model] / $peak_num_per_corp[$id][$model];	
				$peak_T2_rate_per_corp[$id][$model] = $peak_T2_error_num_per_corp[$id][$model] / $peak_num_per_corp[$id][$model];
				
			}
		
			if ($valley_num_per_corp[$id][$model] == 0)
			{
				$valley_accuracy_per_corp[$id][$model] = 0;
				$valley_T1_rate_per_corp[$id][$model] = 0;
				$valley_T2_rate_per_corp[$id][$model] = 0;
			}
			else
			{
				$valley_accuracy_per_corp[$id][$model] = $predicted_valley_num_per_corp[$id][$model] / $valley_num_per_corp[$id][$model];
				$valley_T1_rate_per_corp[$id][$model] = $valley_T1_error_num_per_corp[$id][$model] / $valley_num_per_corp[$id][$model];
				$valley_T2_rate_per_corp[$id][$model] = $valley_T1_error_num_per_corp[$id][$model] / $valley_num_per_corp[$id][$model];
			}
	
		
			$template->assign_block_vars('corp_list.model', array(
				'MODEL_ID'								=> $model,				
				'CHART_LINK'							=> 'prophet_chart.php?report_path=' . $report_path . '&mode=signal_chart&corp_id=' . $id . '&model=' . $model, 
				
				'SIGNAL_PERCENT'						=> round($signal_percent_per_corp[$id][$model] * 100, 0),		
				'ACCURACY'								=> round($accuracy_per_corp[$id][$model] * 100, 0),	
				'ERROR_RATE'							=> round($error_rate_per_corp[$id][$model] * 100, 0),
				
				'PEAK_ACCURACY'							=> round($peak_accuracy_per_corp[$id][$model] * 100, 0),				
				'PEAK_T1_RATE'							=> round($peak_T1_rate_per_corp[$id][$model] * 100, 0),	
				'PEAK_T2_RATE'							=> round($peak_T2_rate_per_corp[$id][$model] * 100, 0),	
				
				'VALLEY_ACCURACY'						=> round($valley_accuracy_per_corp[$id][$model] * 100, 0),	
				'VALLEY_T1_RATE'						=> round($valley_T1_rate_per_corp[$id][$model] * 100, 0),	
				'VALLEY_T2_RATE'						=> round($valley_T2_rate_per_corp[$id][$model] * 100, 0),			
				
				'MONTHLY_RETURN_RATE'					=> round($monthly_return_rate[$id][$model], 0),
				'TOTAL_RETURN_RATE'						=> round($total_return_rate[$id][$model], 0),
				
				'PRICE_STD_DEV_PEAK'					=> ($price_mean_peak[$id][$model] == 0) ? 0 : round($price_std_dev_peak[$id][$model] * 100 / $price_mean_peak[$id][$model], 1), 
				'PRICE_STD_DEV_VALLEY'					=> ($price_mean_valley[$id][$model] == 0) ? 0 : round($price_std_dev_valley[$id][$model] * 100 / $price_mean_valley[$id][$model], 1), 
				)
			);		
		}
		
		//print_r($signal_percent_per_corp[$id]);
		
	}
	
	
	//
	//	list corps with buy/sell signals 
	//	
	
	
	//
	//										Predicted Signal
	//									1						0
	//							----------------------------------
	//
	//	Real, Generated		1		Correct					T2 Error
	//	Signals						Prediction				(Miss)
	//
	//						0		T1 Error
	//								(False Alarm)
	//
	//
	//	When we have a predicted signal as 1, the prob of that the prediction is correct is:
	//
	//			Correct Prediction
	//		-----------------------------
	//		Correct Prediction + T1 Error
	// 
	//
	//	When we have a predicted signal with pseduo prob, p, to be 1, the prob of that the real signal is 1 is:
	//
	//			Correct Prediction
	//		-----------------------------	x	p  
	//		Correct Prediction + T1 Error
	//
	//
	
	//
	// Based on Kelly's formula, we calculate a weight for each model and each stock. 
	//
	//
	//			p 			q
	// 	f = --------  -  -------
	//			a			b
	//
	//	where 	f: weight, the percentage of wealth to bid
	//			p: prob. to win
	//			q: prob. to lose, 1 - p
	//			b: the net odds received on the wager ("b to 1"); that is, you could win $b (plus the $1 wagered) for a $1 bet
	//			a: the net loss, the wealth decrease from 1 to 1 - a if lose 
	//
	//	We use monthly return rate to be b, and set 10% stop loss as a. 
	//
		
	
	
	$prob_to_be_true 	= array();
	$Kelly_weight		= array();
	$stop_loss			= array();
	
	foreach($sell_signal as $id => $max_prob)
	{
		// we calculate the prob that the real signal is sell: with model accuracy and psedu-prob generated by SPSS
		$prob_to_be_true[$id] 	= array();
		$Kelly_weight[$id]		= array();
		$stop_loss[$id]			= array();
		
		for($i = 0; $i < 6; ++$i)
		{
			$model = $model_id[$i];
			
			if (($predicted_peak_num_per_corp[$id][$model] + $peak_T1_error_num_per_corp[$id][$model]) == 0)
			{
				//echo "zero!! buy $id $model <BR>";
				$prob_to_be_true[$id][$model] = 0;
			}
			else
				$prob_to_be_true[$id][$model] = round($sell_signal_prob[$id][$i] * $predicted_peak_num_per_corp[$id][$model] / ($predicted_peak_num_per_corp[$id][$model] + $peak_T1_error_num_per_corp[$id][$model]), 0);
			
			if ($price_mean_peak[$id][$model] != 0)
				$stop_loss[$id][$model] = 1.5 * $price_std_dev_peak[$id][$model] / $price_mean_peak[$id][$model];
			else
				$stop_loss[$id][$model] = 0.1;
				
			if ( $stop_loss[$id][$model] < 0.1)
				$stop_loss[$id][$model] = 0.1;
			else if ($stop_loss[$id][$model] > 0.5)
				$stop_loss[$id][$model] = 0.5;
				
			if ($monthly_return_rate[$id][$model] != 0)
				$Kelly_weight[$id][$model]	= ($prob_to_be_true[$id][$model] / $stop_loss[$id][$model]) -  ((100 - $prob_to_be_true[$id][$model]) / $monthly_return_rate[$id][$model]);
			else
				$Kelly_weight[$id][$model]	= 0;
			
		}
		
		$sell_signal[$id] = max($Kelly_weight[$id]);
	}
	
	//print_r($buy_signal_prob);
	foreach($buy_signal as $id => $max_prob)
	{
		// we calculate the prob that the real signal is buy: with model accuracy and psedu-prob generated by SPSS
		$prob_to_be_true[$id] = array();
		$Kelly_weight[$id]		= array();
		$stop_loss[$id]			= array();
		
		for($i = 0; $i < 6; ++$i)
		{
			$model = $model_id[$i];
			
			if (($predicted_valley_num_per_corp[$id][$model] + $valley_T1_error_num_per_corp[$id][$model]) == 0)
			{
				//echo "zero!! buy $id $model <BR>";
				$prob_to_be_true[$id][$model] = 0;
			}
			else
				$prob_to_be_true[$id][$model] = round($buy_signal_prob[$id][$i] * $predicted_valley_num_per_corp[$id][$model] / ($predicted_valley_num_per_corp[$id][$model] + $valley_T1_error_num_per_corp[$id][$model]), 0);

			if ($price_mean_peak[$id][$model] != 0)
				$stop_loss[$id][$model] = 1.5 * $price_std_dev_peak[$id][$model] / $price_mean_peak[$id][$model];
			else 
				$stop_loss[$id][$model] = 0.1;
				
			if ( $stop_loss[$id][$model] < 0.1)
				$stop_loss[$id][$model] = 0.1;
			else if ($stop_loss[$id][$model] > 0.5)
				$stop_loss[$id][$model] = 0.5;
				
			if ($monthly_return_rate[$id][$model] != 0)
				$Kelly_weight[$id][$model]	= ($prob_to_be_true[$id][$model] / $stop_loss[$id][$model]) -  ((100 - $prob_to_be_true[$id][$model]) / $monthly_return_rate[$id][$model]);
			else
				$Kelly_weight[$id][$model]	= 0;
		}
		
		$buy_signal[$id] = max($Kelly_weight[$id]);
	}
	
	
	// sort by value
	arsort($buy_signal);
	arsort($sell_signal);
	
	
	//
	// output analysis result 
	//
	
	foreach($sell_signal as $id => $max_prob)
	{		
			
		$template->assign_block_vars('sell_corp_list', array(
		'CORP_ID'						=> $id,		
		'CORP_NAME'						=> get_corp_chinese_name($id),	
		'PROB_A'						=> $sell_signal_prob[$id][0],
		'PROB_B'						=> $sell_signal_prob[$id][1],
		'PROB_C'						=> $sell_signal_prob[$id][2],
		'PROB_D'						=> $sell_signal_prob[$id][3],
		'PROB_E'						=> $sell_signal_prob[$id][4],
		'PROB_F'						=> $sell_signal_prob[$id][5],		
		'MAX_PROB'						=> $max_prob,	
		'PROB_A_TO_BE_TRUE'						=> $prob_to_be_true[$id]['A'],
		'PROB_B_TO_BE_TRUE'						=> $prob_to_be_true[$id]['B'],
		'PROB_C_TO_BE_TRUE'						=> $prob_to_be_true[$id]['C'],
		'PROB_D_TO_BE_TRUE'						=> $prob_to_be_true[$id]['D'],
		'PROB_E_TO_BE_TRUE'						=> $prob_to_be_true[$id]['E'],
		'PROB_F_TO_BE_TRUE'						=> $prob_to_be_true[$id]['F'],		
				
		'MONTHLY_RETURN_RATE_A'						=> round($monthly_return_rate[$id]['A'], 0),
		'MONTHLY_RETURN_RATE_B'						=> round($monthly_return_rate[$id]['B'], 0),
		'MONTHLY_RETURN_RATE_C'						=> round($monthly_return_rate[$id]['C'], 0),
		'MONTHLY_RETURN_RATE_D'						=> round($monthly_return_rate[$id]['D'], 0),
		'MONTHLY_RETURN_RATE_E'						=> round($monthly_return_rate[$id]['E'], 0),
		'MONTHLY_RETURN_RATE_F'						=> round($monthly_return_rate[$id]['F'], 0),
		
		'KELLY_WEIGHT_A'							=> round($Kelly_weight[$id]['A'], 1),
		'KELLY_WEIGHT_B'							=> round($Kelly_weight[$id]['B'], 1),
		'KELLY_WEIGHT_C'							=> round($Kelly_weight[$id]['C'], 1),
		'KELLY_WEIGHT_D'							=> round($Kelly_weight[$id]['D'], 1),
		'KELLY_WEIGHT_E'							=> round($Kelly_weight[$id]['E'], 1),
		'KELLY_WEIGHT_F'							=> round($Kelly_weight[$id]['F'], 1),
		
		'STOP_LOSS_A'								=> round($stop_loss[$id]['A'] * 100, 1),
		'STOP_LOSS_B'								=> round($stop_loss[$id]['B'] * 100, 1),
		'STOP_LOSS_C'								=> round($stop_loss[$id]['C'] * 100, 1),
		'STOP_LOSS_D'								=> round($stop_loss[$id]['D'] * 100, 1),
		'STOP_LOSS_E'								=> round($stop_loss[$id]['E'] * 100, 1),
		'STOP_LOSS_F'								=> round($stop_loss[$id]['F'] * 100, 1),
		
		
		
		)
		);	
	}
	
	//print_r($buy_signal_prob);
	foreach($buy_signal as $id => $max_prob)
	{
	
		$template->assign_block_vars('buy_corp_list', array(
		'CORP_ID'						=> $id,		
		'CORP_NAME'						=> get_corp_chinese_name($id),	
		
		'PROB_A'						=> $buy_signal_prob[$id][0],
		'PROB_B'						=> $buy_signal_prob[$id][1],
		'PROB_C'						=> $buy_signal_prob[$id][2],
		'PROB_D'						=> $buy_signal_prob[$id][3],
		'PROB_E'						=> $buy_signal_prob[$id][4],
		'PROB_F'						=> $buy_signal_prob[$id][5],	
		'MAX_PROB'						=> $max_prob,	
		'PROB_A_TO_BE_TRUE'						=> $prob_to_be_true[$id]['A'],
		'PROB_B_TO_BE_TRUE'						=> $prob_to_be_true[$id]['B'],
		'PROB_C_TO_BE_TRUE'						=> $prob_to_be_true[$id]['C'],
		'PROB_D_TO_BE_TRUE'						=> $prob_to_be_true[$id]['D'],
		'PROB_E_TO_BE_TRUE'						=> $prob_to_be_true[$id]['E'],
		'PROB_F_TO_BE_TRUE'						=> $prob_to_be_true[$id]['F'],		
		
		'MONTHLY_RETURN_RATE_A'						=> round($monthly_return_rate[$id]['A'], 0),
		'MONTHLY_RETURN_RATE_B'						=> round($monthly_return_rate[$id]['B'], 0),
		'MONTHLY_RETURN_RATE_C'						=> round($monthly_return_rate[$id]['C'], 0),
		'MONTHLY_RETURN_RATE_D'						=> round($monthly_return_rate[$id]['D'], 0),
		'MONTHLY_RETURN_RATE_E'						=> round($monthly_return_rate[$id]['E'], 0),
		'MONTHLY_RETURN_RATE_F'						=> round($monthly_return_rate[$id]['F'], 0),
		
		'KELLY_WEIGHT_A'							=> round($Kelly_weight[$id]['A'], 1),
		'KELLY_WEIGHT_B'							=> round($Kelly_weight[$id]['B'], 1),
		'KELLY_WEIGHT_C'							=> round($Kelly_weight[$id]['C'], 1),
		'KELLY_WEIGHT_D'							=> round($Kelly_weight[$id]['D'], 1),
		'KELLY_WEIGHT_E'							=> round($Kelly_weight[$id]['E'], 1),
		'KELLY_WEIGHT_F'							=> round($Kelly_weight[$id]['F'], 1),
		
		'STOP_LOSS_A'								=> round($stop_loss[$id]['A'] * 100, 1),
		'STOP_LOSS_B'								=> round($stop_loss[$id]['B'] * 100, 1),
		'STOP_LOSS_C'								=> round($stop_loss[$id]['C'] * 100, 1),
		'STOP_LOSS_D'								=> round($stop_loss[$id]['D'] * 100, 1),
		'STOP_LOSS_E'								=> round($stop_loss[$id]['E'] * 100, 1),
		'STOP_LOSS_F'								=> round($stop_loss[$id]['F'] * 100, 1),
		)
		);	
	}
}
?>

